from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from starlette.middleware.base import BaseHTTPMiddleware

from src.config import Settings

from .context import set_request_id_middleware
from .token import refresh_token_middleware


def setup_middlewares(app: FastAPI, config: Settings) -> None:
    origins = [
        config.api.frontend_url,
    ]

    app.add_middleware(
        CORSMiddleware,
        allow_origins=origins,
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )

    app.add_middleware(BaseHTTPMiddleware, dispatch=set_request_id_middleware)
    app.add_middleware(BaseHTTPMiddleware, dispatch=refresh_token_middleware)
